package main

import (
	"net/http"
	"fmt"
	"html/template"
	"log"
	"strings"
	"time"
	"crypto/md5"
	"io"
	"strconv"
)

func hello(w http.ResponseWriter, r *http.Request)  {
	r.ParseForm()
	fmt.Println("begin....")
	fmt.Println(r.Form)
	fmt.Println(r.PostForm)
	fmt.Println("path:",r.URL.Path)
	fmt.Println("scheme",r.URL.Host)
	for k,v := range r.Form{
		fmt.Println("key:",k)
		fmt.Println("val",strings.Join(v,""))
	}
	fmt.Fprint(w,"hello...")
	fmt.Println("end...")
}
func login(w http.ResponseWriter,r *http.Request){
	fmt.Println("method",r.Method)
	if r.Method=="GET"{
		curtime := time.Now().Unix()
		h := md5.New()
		io.WriteString(h,strconv.FormatInt(curtime,10))
		token := fmt.Sprintf("%x",h.Sum(nil))
		t,_ := template.ParseFiles("./login.gtpl")
		log.Println(t.Execute(w,token))
	}else{
		r.ParseForm()
		//请求的是登录数据，那么执行登录的逻辑判断
		fmt.Println("username:", r.Form["username"])
		fmt.Println("password:", r.Form["password"])
		template.HTMLEscape(w,[]byte(r.Form.Get("username")))
	}
}
func main(){
	http.HandleFunc("/",hello)
	http.HandleFunc("/login",login)
	err := http.ListenAndServe(":9090",nil)
	if err != nil{
		log.Fatal("ListenAndServer",err)
	}

}